package com.yida.wy.vote.outside.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.mybatisflex.annotation.UseDataSource;
import com.mybatisflex.core.query.QueryWrapper;
import com.yida.common.core.exception.ServiceException;
import com.yida.common.core.utils.StringUtils;
import com.yida.wy.common.constant.WyConstants;
import com.yida.wy.common.domain.WyBaseBuild;
import com.yida.wy.common.domain.WyBaseCommunity;
import com.yida.wy.common.domain.WyBaseHouse;
import com.yida.wy.common.domain.WyBaseOwner;
import com.yida.wy.common.service.IWyBaseBuildService;
import com.yida.wy.common.service.IWyBaseCommunityService;
import com.yida.wy.common.service.IWyBaseHouseService;
import com.yida.wy.common.service.IWyBaseOwnerService;
import com.yida.wy.common.utils.WyConvertUtil;
import com.yida.wy.vote.outside.domain.dto.WyBaseBuildHouseOwnerDTO;
import com.yida.wy.vote.outside.service.IWyBaseDataService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static com.yida.wy.common.domain.table.WyBaseHouseTableDef.WY_BASE_HOUSE;
import static com.yida.wy.common.domain.table.WyBaseOwnerTableDef.WY_BASE_OWNER;

/**
 * 投票楼栋Service业务层处理
 *
 * @author lqq
 * @date 2023-03-22
 */
@Service
@UseDataSource("FileDs")
public class WyBaseDataServiceImpl implements IWyBaseDataService {

    @Resource
    private IWyBaseBuildService gwjBaseBuildService;

    @Resource
    private IWyBaseHouseService gwjBaseHouseService;

    @Resource
    private IWyBaseOwnerService gwjBaseOwnerService;

    @Resource
    private IWyBaseCommunityService gwjBaseCommunityService;

    /**
     * 根据楼栋ID查询所有数据（楼栋、房屋、业主）
     *
     * @param buildIds 楼栋ID集合
     * @param areaId   地区ID
     * @return /
     */
    @Override
    public WyBaseBuildHouseOwnerDTO getAllDataByBuildIds(Long[] buildIds, Long areaId) {
        //从基础库查询楼栋
        List<WyBaseBuild> baseBuilds = gwjBaseBuildService.listByIds(ListUtil.toList(buildIds));
        //剔除公用户
        baseBuilds.removeIf(b -> WyConstants.YES.equals(b.getPublicFlag()));
        if (StringUtils.isEmpty(baseBuilds)) {
            throw new ServiceException("查无基础楼栋数据");
        }
        //查询基础房屋
        List<String> buildCodes = baseBuilds.stream().map(WyBaseBuild::getBuildCode).collect(Collectors.toList());
        List<WyBaseHouse> baseHouses = gwjBaseHouseService.list(QueryWrapper.create().where(WY_BASE_HOUSE.BUILD_CODE.in(buildCodes).and(WY_BASE_HOUSE.AREA_ID.eq(areaId))));
        //剔除公用户
        baseHouses.removeIf(h -> WyConstants.YES.equals(h.getPublicFlag()));
        if (StringUtils.isEmpty(baseHouses)) {
            throw new ServiceException("查无基础房屋数据");
        }
        //查询基础业主
        List<WyBaseOwner> baseOwners = new ArrayList<>();
        List<String> houseCodes = baseHouses.stream().map(WyBaseHouse::getHouseCode).collect(Collectors.toList());
        //分段查询
        List<List<String>> codeList = WyConvertUtil.partition(houseCodes, 100);
        for (List<String> code : codeList) {
            List<WyBaseOwner> owners = gwjBaseOwnerService.list(QueryWrapper.create().where(WY_BASE_OWNER.HOUSE_CODE.in(code).and(WY_BASE_OWNER.AREA_ID.eq(areaId))));
            if (StringUtils.isNotEmpty(owners)) {
                baseOwners.addAll(owners);
            }
        }
        if (StringUtils.isEmpty(baseOwners)) {
            throw new ServiceException("查无基础业主数据");
        }
        baseOwners.forEach(o -> {
            if (StringUtils.isEmpty(o.getOwnerPhone())) {
                throw new ServiceException("请完善业主信息");
            }
        });
        WyBaseBuildHouseOwnerDTO dto = new WyBaseBuildHouseOwnerDTO();
        dto.setBaseBuilds(baseBuilds);
        dto.setBaseHouses(baseHouses);
        dto.setBaseOwners(baseOwners);
        return dto;
    }

    /**
     * 查询基础数据（小区信息）
     *
     * @param id 基础数据（小区信息）主键
     * @return 基础数据（小区信息）
     */
    @Override
    public WyBaseCommunity selectGwjBaseCommunityByGwjBaseCommunityId(Long id) {
        return gwjBaseCommunityService.getById(id);
    }
}
